# encoding: utf-8
# @File  : test06_order.py
# @Author: wu shaofan
# @Date  :  2024/04/28
# @Desc :
import json
import allure
from faker import Faker
import config
import conftest
from PO.page.page_cart import PageCart
from PO.page.page_index import PageIndex
from PO.page.page_login import PageLogin
from PO.page.page_user_info import PageUserinfo
from Utils.get_driver import GetDriver
from Utils.record_log import Log


@allure.feature('2、购物车模块')
class TestOrder:
    def setup_class(self):
        # 获取driver
        self.driver = GetDriver().get_driver(config.login_url)
        # 异常截图
        conftest.driver = self.driver
        # 获取登录页面对象
        self.login = PageLogin(self.driver)
        # 获取用户个人中心对象
        self.userinfo = PageUserinfo(self.driver)
        # 获取首页对象
        self.index = PageIndex(self.driver)
        # 获取购物车对象
        self.cart = PageCart(self.driver)
        # 获取用户个人中心对象
        self.userinfo = PageUserinfo(self.driver)
        # TODO 创建一个Faker对象
        self.fake = Faker(locale='zh_CN')
        # 获取日志对象
        self.log = Log("商品下订单结算用例").get_logger()

    def teardown_class(self):
        # 关闭driver
        GetDriver().close_driver()
        Log("商品下订单结算用例").write_empty_line()

    @allure.story("2、商品下订单")
    def test_order(self):
        with allure.step("前置条件：登录"):
            # 输入用户名, 密码, 验证码, 点击登录按钮
            self.login.login_user()

        with allure.step("测试步骤一：点击我的购物车按钮"):
            # self.cart.base_refresh()
            self.userinfo.page_click_my_shopping_cart_btn()

        with allure.step("测试步骤二：购物车页面中搜索框搜索商品：小米8"):
            goods_name = "小米8"
            self.cart.page_input_search_goods(goods_name)

        with allure.step("测试步骤三：点击搜索按钮"):
            self.cart.page_click_search_button()

        with allure.step("测试步骤四：滚动滑动条向下滑"):
            self.cart.base_move_scroll_bar(200)

        with allure.step("测试步骤五：点击加入购物车按钮"):
            self.cart.page_click_add_cart_button()

        with allure.step("测试步骤七：进入iframe框架"):
            self.index.page_into_iframe01()

        with allure.step("测试步骤八：获取加入购物车成功提示信息"):
            result = self.index.page_get_add_cart_success_info()
            self.login.base_get_allure_image()
            # TODO 退出iframe框架
            self.driver.switch_to.default_content()

        with allure.step("测试步骤九：点击关闭弹框按钮"):
            self.index.page_click_close_btn()

        with allure.step("测试步骤十：点击去购物车按钮"):
            self.index.page_click_cart_btn()

        with allure.step("测试步骤十二：勾选指定的商品"):
            self.cart.page_click_select_goods(goods_name)

        with allure.step("测试步骤十三：点击去结算按钮"):
            self.cart.page_click_settle_accounts_button()

        with allure.step("测试步骤十四：输入订单备注信息"):
            order_remarks = self.fake.sentence(nb_words=6)
            self.cart.page_input_order_remarks(order_remarks)

        with allure.step("测试步骤十五：滑动滚动条向下滑"):
            self.cart.base_move_scroll_bar(1100)

        with allure.step("测试步骤十六：输入支付密码"):
            self.cart.page_input_pay_password("wsf123456")

        with allure.step("测试步骤十七：点击提交订单按钮"):
            self.cart.page_click_submit_order_button()

        with allure.step("测试步骤十八：获取提交订单成功Toast提示信息"):
            toast = self.cart.page_get_submit_order_success_toast()
            self.login.base_get_allure_image()

        with allure.step("测试步骤十九：点击进入订单详情"):
            self.cart.page_click_order_detail()

        with allure.step("测试步骤二十：获取订单号"):
            order_number = self.cart.page_get_order_number()
            self.login.base_get_allure_image()

        # with allure.step("测试步骤二十一：获取收货人信息"):
        #     receiver_info = self.cart.page_get_consignee_info()

        with allure.step("测试步骤二十二：点击取消订单"):
            self.cart.page_click_cancel_order()

        with allure.step("测试步骤二十三：确定取消订单"):
            text = self.cart.page_click_confirm_cancel_order()
            self.login.base_get_allure_image()

        with allure.step("测试步骤二十四：获取“已取消文本"):
            is_cancel_order = self.cart.page_get_cancel_order_success_text()
            self.login.base_get_allure_image()

        # 断言
        assert "订单提交成功!" == toast
        assert "操作成功" == text
        assert "已取消" == is_cancel_order

        data = {
            "用户信息": config.testdata,
            "订单内容": {
                "商品名称": goods_name,
                "商品是否添加成功": result,
                "订单备注信息": order_remarks,
                "toast信息": toast},
            "取消订单": {
                "订单号": order_number,
                "是否取消成功": text,
                "订单状态": is_cancel_order}
            }
        # print(data)
        self.log.info(f"测试数据：{data}")
        data = json.dumps(data, ensure_ascii=False, indent=4)
        allure.attach(data, "测试数据", allure.attachment_type.JSON)
